package com.example.a086.customkeybord;

import android.inputmethodservice.KeyboardView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    private Keyboard_Util keyboardUtil;
    private EditText et_searchtxt;
    private EditText et_province;
    private EditText et_city;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_province=findViewById(R.id.et_province);
        et_city=findViewById(R.id.et_city);
        et_searchtxt = (EditText) findViewById(R.id.et_searchtxt);
        et_province.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                    keyboardUtil = new Keyboard_Util(MainActivity.this, et_province);
                    keyboardUtil.hideSoftInputMethod();
                    keyboardUtil.changeKeybord(0);
                    keyboardUtil.showKeyboard();
                return false;
            }
        });
        et_province.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                et_city.requestFocus();
                et_city.setFocusable(true);
                keyboardUtil = new Keyboard_Util(MainActivity.this, et_city);
                keyboardUtil.hideSoftInputMethod();
                keyboardUtil.changeKeybord(1);
                keyboardUtil.showKeyboard();
            }
        });
        et_city.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {

                    keyboardUtil = new Keyboard_Util(MainActivity.this, et_city);
                    keyboardUtil.hideSoftInputMethod();
                    keyboardUtil.changeKeybord(1);
                    keyboardUtil.showKeyboard();
                return false;
            }
        });

        et_city.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length()>0){
                    et_searchtxt.requestFocus();
                    et_searchtxt.setFocusable(true);
                    keyboardUtil = new Keyboard_Util(MainActivity.this, et_searchtxt);
                    keyboardUtil.hideSoftInputMethod();
                    keyboardUtil.changeKeybord(2);
                    keyboardUtil.showKeyboard();
                }
            }
        });

        et_searchtxt.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                keyboardUtil = new Keyboard_Util(MainActivity.this, et_searchtxt);
                keyboardUtil.hideSoftInputMethod();
                keyboardUtil.changeKeybord(2);
                keyboardUtil.showKeyboard();

                return false;
            }
        });

    }
}
